import api from '@/libs/request'
import { IcFreightFee } from './sameCityOrder/types'

/**
 * 根据订单状态查询订单
 */
export const GetShopInfo = (status, current, size) => {
    return api.get(`gruul-mall-order/order?status=${status}&current=${current}&size=${size}&isMiniAppTip=true`)
}
/**
 * 根据订单状态查询订单
 */
export const SearchShopInfo = (status, current, size, keyword) => {
    return api.get(`gruul-mall-order/order?status=${status}&current=${current}&size=${size}&productName=${keyword}`)
}
/**
 * 物流轨迹
 */
export const doGetLogisticsDetails = () => {
    return api.get('gruul-mall-freight/logistics/node?companyCode=shengfeng&waybillNo=SF1357038560440')
}
/**
 * 获取订单列表 usePackage 是否根据
 * @param usePackage true 按包裹查询
 * @param packageId 为空查询未发货 否则查询已发货
 */
export const doGetOrderList = (params, orderNo?: string) => {
    if (orderNo) {
        return api.get(`gruul-mall-order/order/${orderNo}`, params)
    }
    return api.get(`gruul-mall-order/order`, params)
}
/**
 * 通过orderNo获取订单详情
 * @param {*} orderId
 */
export const doGetOrderDetails = (orderNo: string, params: any) => {
    return api.get(`/gruul-mall-order/order/${orderNo}`, params)
}
/**
 * 通过orderNo获取未发货订单
 * @param {string} orderNo
 * @param {string} shopOrderNo
 */
export const dogetOrderNotDetailsByOrderNo = (orderNo: string, shopOrderNo: string) => {
    return api.get(`/gruul-mall-order/order/${orderNo}/shopOrder/${shopOrderNo}/undelivered`)
}
/**
 * 查询所有已发货的商品
 * @param {string} orderNo
 * @param {string} shopOrderNo
 */
export const doGetDeliveryPageAll = (orderNo: string, shopOrderNo: string) => {
    return api.get(`gruul-mall-order/order/${orderNo}/shopOrder/${shopOrderNo}/delivered`)
}
/**
 * 商品发货
 * @param {string} orderNo
 */
export const doPutOrderDetailsByOrderNo = (orderNo: string, data) => {
    return api.put(`/gruul-mall-order/order/${orderNo}/deliver`, data)
}

const deliverTypeOptionsMap = {
    IC_MERCHANT: '商家配送',
    IC_OPEN: 'UU 跑腿配送',
}
/**
 * 获取店铺可选择的同城配送方式
 */
export const doGetShopDeliveryMethod = async () => {
    const { code, data, msg } = await api.post<('IC_MERCHANT' | 'IC_OPEN')[]>(`addon-ic/ic/shop/order/deliver/type`)
    if (code === 200 && data) {
        return {
            data: data.map((item) => {
                return {
                    label: deliverTypeOptionsMap[item],
                    value: item,
                }
            }),
            code,
            msg,
        }
    } else {
        return {
            data: [],
            code,
            msg,
        }
    }
}
/**
 * 批量获取配送单运费价格 当选择UU 跑腿作为配送方时可用
 */
export const doGetBatchDeliveryPrice = (data: string[]) => {
    return api.post<IcFreightFee>('addon-ic/ic/shop/order/deliver/price', data)
}

/**
 * 商品批量发货
 * @param {string} orderNo
 */
export const doPostOrderBatchDeliver = (data) => {
    return api.put(`gruul-mall-order/order/batch/deliver`, data)
}
/**
 * 关闭店铺订单
 * @param {string} orderNo
 * @param {string} shopOrderNo
 */
export const doPutCloseShopOrder = (orderNo: string, shopOrderNo: string) => {
    return api.put(`gruul-mall-order/order/${orderNo}/shopOrder/${shopOrderNo}/close`)
}
export const doGetOrderInfo = (orderNo: string, shopId?: string, packageId?: string, usePackage = true) => {
    const params: any = { shopId, usePackage }
    !!packageId && (params.packageId = packageId)
    return api.get(`gruul-mall-order/order/${orderNo}`, params)
}

/**
 * 根据模块类id获取配置信息
 */
export const queryConfigByModule = (params: string): Promise<any> => {
    return api.get(`gruul-mall-addon-platform/platform/config/query-config-by-module?modules=${params}`)
}
